package com.dhcc.cdc.busi.controller;


import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.cdc.busi.entity.CdcDiagnosisEntity;
import com.dhcc.cdc.busi.service.ICdcDiagnosisService;
import com.dhcc.core.framework.annotion.BussinessLog;
import com.dhcc.core.framework.annotion.Permission;
import com.dhcc.core.framework.base.controller.BaseController;
import com.dhcc.core.framework.base.warpper.EmptyWarpper;
import com.dhcc.core.framework.exception.BizException;
import com.dhcc.core.framework.exception.BizExceptionEnum;
import com.dhcc.core.framework.result.R;
import com.dhcc.core.framework.result.page.PageFactory;
import com.dhcc.core.framework.util.CommonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * 诊断
 * 
 * @ClassName: CdcDiagnosisController
 * @Description: TODO
 * @author jumper
 * @date 2021-03-15 10:20:08
 */
@Controller
@RequestMapping("${sys.backendPath}/cdc/cdcdiagnosis")
public class CdcDiagnosisController extends BaseController {

	private String PREFIX = "/backend/cdc/cdcdiagnosis/";

	@Autowired
	private ICdcDiagnosisService cdcDiagnosisService;
	

    /**
     * 跳转到诊断列表首页
     */
    @RequestMapping("")
    @Permission("cdcdiagnosis:list")
    public String index() {
        return PREFIX + "cdcdiagnosis.html";
    }
	
    /**
     * 跳转到新增诊断
     */
    @RequestMapping("/to_add")
    @Permission("cdcdiagnosis:add")
    public String toAdd() {
        return PREFIX + "cdcdiagnosis_add.html";
    }
	
	/**
     * 跳转到修改诊断
     */
    @RequestMapping("/to_update/{cdcDiagnosisId}")
    @Permission("cdcdiagnosis:update")
    public String toUpdate(@PathVariable Long cdcDiagnosisId, Model model) {
        CdcDiagnosisEntity cdcDiagnosis = this.cdcDiagnosisService.selectById(cdcDiagnosisId);
        model.addAttribute("cdcDiagnosis",cdcDiagnosis);
        return PREFIX + "cdcdiagnosis_edit.html";
    }

    /**
     * 获取诊断列表
     */
	@SuppressWarnings("unchecked")
    @RequestMapping(value = "/list")
    @ResponseBody
    @Permission("cdcdiagnosis:list")
    public Object list(@RequestParam Map<String, Object> params) {
        List<CdcDiagnosisEntity> result = null;
        Page<CdcDiagnosisEntity> page = new PageFactory<CdcDiagnosisEntity>().defaultPage();
        if((CommonUtil.isNotEmpty(params.get("NotQuery")))&&(params.get("NotQuery").toString().equals("true"))){
            result = new ArrayList<CdcDiagnosisEntity>();
        }else{
            result = this.cdcDiagnosisService.page(page,params);
        }
        page.setRecords((List<CdcDiagnosisEntity>) new EmptyWarpper(result).warp());
        return R.packForBT(page);
    }

    /**
     * 新增诊断
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    @Permission("cdcdiagnosis:add")
    @BussinessLog(value = "新增诊断")
    public Object add(CdcDiagnosisEntity cdcDiagnosis) {
        if (CommonUtil.isOneEmpty(cdcDiagnosis)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        cdcDiagnosis.insert();
        return R.ok();
    }

    /**
     * 删除诊断
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    @Permission("cdcdiagnosis:delete")
    @BussinessLog(value = "删除诊断")
    public Object delete(@RequestParam Long cdcDiagnosisId) {
	
        this.cdcDiagnosisService.deleteById(cdcDiagnosisId);

        return R.ok();
    }

    /**
     * 修改诊断
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    @Permission("cdcdiagnosis:update")
    @BussinessLog(value = "修改诊断")
    public Object update(CdcDiagnosisEntity cdcDiagnosis) {
        if (CommonUtil.isOneEmpty(cdcDiagnosis)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        CdcDiagnosisEntity old = this.cdcDiagnosisService.selectById(cdcDiagnosis.getId());

        old.setEpisodeId(cdcDiagnosis.getEpisodeId());
        old.setBusDiagCateId(cdcDiagnosis.getBusDiagCateId());
        old.setBusDiagTypeId(cdcDiagnosis.getBusDiagTypeId());
        old.setDescription(cdcDiagnosis.getDescription());
        old.setIcd10(cdcDiagnosis.getIcd10());
        old.setIsMain(cdcDiagnosis.getIsMain());
        old.setBusDiagStatusId(cdcDiagnosis.getBusDiagStatusId());
        old.setDiagDate(cdcDiagnosis.getDiagDate());
        old.setAttackDate(cdcDiagnosis.getAttackDate());
        old.setBusDiagSourceId(cdcDiagnosis.getBusDiagSourceId());
        old.setNote(cdcDiagnosis.getNote());
        old.setDataId(cdcDiagnosis.getDataId());
        old.setIsActive(cdcDiagnosis.getIsActive());
        old.updateAllColumnById();
        return R.ok();
    }
	
}
